package com.carful.xigua.modules.user.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.carful.xigua.modules.common.entity.BaseEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.carful.xigua.modules.user.dto.request.ReqGetUser;
import com.carful.xigua.modules.user.dto.request.ReqLoginUser;
import com.carful.xigua.modules.user.dto.request.ReqUser;
import com.carful.xigua.modules.user.dto.response.ResUser;
import io.github.linpeilie.annotations.AutoMapper;
import io.github.linpeilie.annotations.AutoMappers;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.lang.Nullable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 
 * </p>
 *
 * @author ad
 * @since 2024-07-11
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@TableName("carful_user")
@AutoMappers(value = {
        @AutoMapper(target = ReqLoginUser.class),
        @AutoMapper(target = ReqGetUser.class),
        @AutoMapper(target = ReqUser.class),
        @AutoMapper(target = ResUser.class),
})
public class User extends BaseEntity<User> {

    /**
     * 用户名
     */
    @TableField("username")
    private String username;

    @TableField("user_phone")
    private String userPhone;

    @TableField("member")
    private String member;

    @TableField("HYJF")
    private Double HYJF;

    @TableField("member_name")
    private String memberName;

    @TableField("cnt")
    private Integer cnt;

    @TableField("current_cnt")
    private Integer currentCnt;

    /**
     * 姓名
     */
    @TableField("real_name")
    private String realName;

    /**
     * 密码
     */
    @TableField("password")
    private String password;

    /**
     * 微信openid
     */
    @TableField("openid")
    private String openid;

    @TableField("org_id")
    private Integer orgId;

    @TableField("status")
    private Integer status;

    @Override
    public Serializable pkVal() {
        return null;
    }



    @TableField(exist = false)
    private String orgName;

    @TableField(exist = false)
    @Nullable
    private List<Role> roles;

    public List<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> authorities = new ArrayList<>();
        if (null != roles) {
            for (Role role : roles) {
                if (StringUtils.hasText(role.getValue())) {
                    authorities.add(new SimpleGrantedAuthority("ROLE_" + role.getValue()));
                }
                if (null == role.getAuths()) continue;
                for (Auth auth : role.getAuths()) {
                    if (StringUtils.hasText(auth.getValue())) {
                        authorities.add(new SimpleGrantedAuthority(auth.getValue()));
                    }
                }
            }
        }
        if (authorities.size() == 0 && "dev".equals(getUsername())) {
            authorities.add(new SimpleGrantedAuthority("ROLE_admin"));
        }
        return authorities;
    }
}
